{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Notebook 1\n",
    "#### 1.1 \n",
    "Buscar `convert` y `parse` en la documentación."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "?convert;\n",
    "# ?parse"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 1.2 \n",
    "Asignar `365` a una variable llamada `days`. Convierte `days` a flotante."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "days = 365\n",
    "convert(Float64, days)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 1.3 \n",
    "Ver que sucede ejectuando \n",
    "\n",
    "```julia\n",
    "convert(Int64, '1')\n",
    "```\n",
    "and\n",
    "\n",
    "```julia\n",
    "parse(Int64, '1')\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Esto regresa el código ascii (un entero) asociado con el caracter '1'\n",
    "convert(Int64, '1')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Esto regresa un entero encapsulado entre comillas\n",
    "parse(Int64, '1')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Notebook 2\n",
    "#### 2.1 \n",
    "Crea una cadena que dice \"hola\" 1000 veces"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "\"hola\"^1000"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 2.2\n",
    "Agrega dos números dentro de una cadena"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "m, n = 1, 1\n",
    "\"$m + $n = $(m + n)\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Notebook 3\n",
    "\n",
    "#### 3.1 \n",
    "Crea un arreglo, `arreglo`, que es un arreglo en 1D de 2 elementos, cada uno conteniendo el número 0.\n",
    "Agrega a `arreglo` para agregar un segundo número, `1`, a cada arreglo."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "a_ray = [[0], [0]]\n",
    "push!(a_ray[1], 1)\n",
    "push!(a_ray[2], 1)\n",
    "a_ray"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.2 \n",
    "Trata de agregar \"Emergencia\" a `miagenda` con el valor `911`. Trata de agregar `911` como un entero y no como cadena. ¿Porqué no funciona?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "miagenda = Dict(\"Jenny\" => \"867-5309\", \"Ghostbusters\" => \"555-2368\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "miagenda[\"Emergency\"] = 911\n",
    "#= \n",
    "\n",
    "Julia infiere que \"miagenda\" toma ambos llaves\n",
    "y valores del tipo \"String\". Podemos ver que miagenda\n",
    "es un Dict{String,String} con 2 entradas. Esto significa que\n",
    "Julia no va a aceptar enteros como valores en miagenda.\n",
    "\n",
    "=#"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 3.3 \n",
    "Crea un nuevo diccionario que se llame `agenda_flexible` que tenga el número de Jenny guardado como cadena y el de los Cazafantasmas como entero."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "agenda_flexible = Dict(\"Jenny\" => \"867-5309\", \"Ghostbusters\" => 5552368)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 3.4\n",
    "Add the key \"Emergency\" with the value `911` (an integer) to `flexible_phonebook`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "flexible_phonebook[\"Emergency\"] = 911"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 3.5\n",
    "3.5 ¿Porqué podemos agregar un entero como valor a `agenda_flexible` pero no a  `miagenda`? ¿Cómo pudimos haber inicializado `miagenda` para que aceptara enteros como valores?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#= \n",
    "\n",
    "Julia infiere que miagenda_flexible toma valores del tipo\n",
    "Any. A diferencia de miagenda, miagenda_flexible es un\n",
    "Dict{String,Any} con 2 entradas.\n",
    "\n",
    "Para evitar esto, podemos inicializar miagenda a un\n",
    "diccionario vacío y agregamos entradas después. O podemos\n",
    "decirle a Julia explícitamente que queremos un diccionario\n",
    "que acepte objectos del tipo Any como valores.\n",
    "\n",
    "¡Ve los ejemplos!\n",
    "\n",
    "=#\n",
    "\n",
    "miagenda = Dict()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "miagenda = Dict{String, Any}(\"Jenny\" => \"867-5309\", \"Ghostbusters\" => \"555-2368\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "### Notebook 4\n",
    "\n",
    "#### 4.1\n",
    "\n",
    "4.1 Crea un diccionario `squares`, que tiene llaves de valores de 1 a 100. El valor asociado a cada llave es el cuadrado de la llave. Guarda los valores asociados a las llaves pares como enteros y las impares como cadenas. Por ejemplo,\n",
    "\n",
    "```julia\n",
    "squares[10] == 100\n",
    "squares[11] == \"121\"\n",
    "```\n",
    "\n",
    "(¡No necesitas condicionales para esto!)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "squares = Dict()\n",
    "iterable = range(1, 2, 50)\n",
    "#  Otra opción \n",
    "# iterable = 1:2:99\n",
    "for key in iterable\n",
    "    squares[key] = \"$(key^2)\"\n",
    "    squares[key + 1] = (key + 1)^2\n",
    "end\n",
    "\n",
    "@show squares[10]\n",
    "@show squares[11]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 4.2\n",
    "\n",
    "4.2 Usa `fill` para crea una matriz de `10x10` de solo `0`'s. Pobla las primeras 10 entradas con el índice de esa entrada. ¿Julia usa el orden de primero columna o primero renglón? (O sea, ¿el \"segundo\" elemento es el de la primera columna en el primer renglón, ó es el de el primer renglón en la segunda columna?)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "A = fill(0, (10, 10))\n",
    "for i in 1:10\n",
    "    A[i] = i\n",
    "end\n",
    "A\n",
    "# ¡Julia usa order columnar! Los primeros 10 elementos van a poblar la primera columna."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Notebook 5\n",
    "\n",
    "#### 5.1\n",
    "\n",
    "5.1 Reescribe FizzBuzz sin usar `elseif`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "N = 16\n",
    "if (N % 3 == 0) & (N % 5 == 0)\n",
    "    println(\"FizzBuzz\")\n",
    "else\n",
    "    if (N % 3 == 0)\n",
    "        println(\"Fizz\")\n",
    "    else\n",
    "        if (N % 5 == 0)\n",
    "            println(\"Buzz\")\n",
    "        else\n",
    "            println(N)\n",
    "        end\n",
    "    end\n",
    "end"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 5.2\n",
    "\n",
    "Reescribe FizzBuzz usando el operador ternario."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true,
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "((N % 3 == 0) & (N % 5 == 0)) ? println(\"FizzBuzz\") : ((N % 3 == 0) ? println(\"Fizz\") : ((N % 5 == 0) ? println(\"Buzz\") : println(N)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Notebook 6\n",
    "\n",
    "#### 6.1\n",
    "\n",
    "6.1 En vez de broadcastear `f` sobre `v`,  pudimos haber hecho `v .^ 2`.\n",
    "\n",
    "Sin declarar una nueva funcion, agrega 1 a cada elemento de una matriz de `3x3` llena de `0`'s."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "fill(0, (3, 3)) .+ 1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 6.2\n",
    "\n",
    "6.2 En vez de broadcastear `f` sobre el vector `v` con la sintaxis de punto, aplica `f` a todos los elementos de`v` usando `map` como función."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "f(x) = x^2\n",
    "v = [1, 2, 3]\n",
    "map(f, v)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 6.3\n",
    "\n",
    "Una cifra de César recorre cada letra un número determinado de plazas más adelante en el abecedario. Un corrimiento, o shift, de 1 manda \"A\" a \"B\". Escribe una función llamada `cesar` que toma una cadena como input y un corrimiento y regresa una cadena desencriptada tal que obtengas\n",
    "\n",
    "```julia\n",
    "cesar(\"abc\", 1)\n",
    "\"bcd\"\n",
    "\n",
    "cesar(\"hello\", 4)\n",
    "\"lipps\"\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "caesar(input_string, shift) = map(x -> x + shift, input_string)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Notebook 7\n",
    "\n",
    "#### 7.1 \n",
    "\n",
    "7.1 Usa el paquete de  (código fuente en https://github.com/JuliaMath/Primes.jl) para encontrar el número primer más grande menor a 1,000,000"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#Pkg.add(\"Primes\")\n",
    "using Primes\n",
    "maximum(primes(1000000))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Notebook 8\n",
    "\n",
    "#### 8.1\n",
    "\n",
    "8.1 Grafica y vs x para `y = x^2` usando el backend de PyPlot."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "using Plots\n",
    "pyplot()\n",
    "x = 1:10\n",
    "y = x .^ 2\n",
    "plot(x, y)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Notebook 9\n",
    "\n",
    "#### 9.1\n",
    "\n",
    "Agrega un método para `+` que aplique un cifrado de César a una cadena (cómo en el notebook 6) tal que\n",
    "\n",
    "```julia\n",
    "\"hello\" + 4 == \"lipps\"\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import Base: +\n",
    "+(x::String, y::Int) = map(x -> x + y, x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 9.2\n",
    "\n",
    "Checa que has extendido propiamente `+` recorriendo la próxima cadena para atrás por 3 letras:\n",
    "\n",
    "\"Gr#qrw#phggoh#lq#wkh#diidluv#ri#gudjrqv#iru#|rx#duh#fuxqfk|#dqg#wdvwh#jrrg#zlwk#nhwfkxs1\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "\"Gr#qrw#phggoh#lq#wkh#diidluv#ri#gudjrqv#iru#|rx#duh#fuxqfk|#dqg#wdvwh#jrrg#zlwk#nhwfkxs1\" + -3"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Notebook 10\n",
    "\n",
    "#### 10.1\n",
    "\n",
    "10.1 Usa `circshift` para obtener una matriz con las columnas de A cíclicamente recorridas a la derecha por 3 columnas.\n",
    "\n",
    "Empezando con\n",
    "\n",
    "```\n",
    "A = [\n",
    " 1  2  3  4  5  6  7  8  9  10\n",
    " 1  2  3  4  5  6  7  8  9  10\n",
    " 1  2  3  4  5  6  7  8  9  10\n",
    " 1  2  3  4  5  6  7  8  9  10\n",
    " 1  2  3  4  5  6  7  8  9  10\n",
    " 1  2  3  4  5  6  7  8  9  10\n",
    " 1  2  3  4  5  6  7  8  9  10\n",
    " 1  2  3  4  5  6  7  8  9  10\n",
    " 1  2  3  4  5  6  7  8  9  10\n",
    " 1  2  3  4  5  6  7  8  9  10\n",
    " ]\n",
    "```\n",
    "\n",
    "Quieres obtener\n",
    "\n",
    "```\n",
    "A = [\n",
    " 7  8  9  10  1  2  3  4  5  6\n",
    " 7  8  9  10  1  2  3  4  5  6\n",
    " 7  8  9  10  1  2  3  4  5  6\n",
    " 7  8  9  10  1  2  3  4  5  6\n",
    " 7  8  9  10  1  2  3  4  5  6\n",
    " 7  8  9  10  1  2  3  4  5  6\n",
    " 7  8  9  10  1  2  3  4  5  6\n",
    " 7  8  9  10  1  2  3  4  5  6\n",
    " 7  8  9  10  1  2  3  4  5  6\n",
    " 7  8  9  10  1  2  3  4  5  6\n",
    " ]\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "circshift(A, (0, 4))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 10.2\n",
    "\n",
    "10.2 Toma el producto de un vector `v` con sí mismo."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "v = [1, 2, 3]\n",
    "v * v'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 10.3\n",
    "10.3 Toma el producto de un vector `v` con sí mismo."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "v' * v"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Notebook 11\n",
    "\n",
    "#### 11.1\n",
    "\n",
    " ¿Cuáles son los eigenvalores de la Matriz A\n",
    "\n",
    "```\n",
    "A =\n",
    "[\n",
    " 140   97   74  168  131\n",
    "  97  106   89  131   36\n",
    "  74   89  152  144   71\n",
    " 168  131  144   54  142\n",
    " 131   36   71  142   36\n",
    "]\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "A =\n",
    "[\n",
    " 140   97   74  168  131\n",
    "  97  106   89  131   36\n",
    "  74   89  152  144   71\n",
    " 168  131  144   54  142\n",
    " 131   36   71  142   36\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "eigdec = eigfact(A)\n",
    "eigdec[:values]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 11.2\n",
    "\n",
    "Crea una matriz diagonal de los eigenvalores de A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "Diagonal(eigdec[:values])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 11.3\n",
    "\n",
    "Realiza un factorización de Hessenberg sobre la matriz A. Verifica que `A = QHQ'`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "F = hessfact(A)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "isapprox(A, F[:Q] * F[:H] * F[:Q]')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Julia 0.6.1",
   "language": "julia",
   "name": "julia-0.6"
  },
  "language_info": {
   "file_extension": ".jl",
   "mimetype": "application/julia",
   "name": "julia",
   "version": "0.6.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
